package com.nanyang.tms.service.waybill.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.flow.invoker.TransOutboundInvoker;
import com.nanyang.tms.api.dto.request.waybill.WaybillAddReuqest;
import com.nanyang.tms.api.dto.request.waybill.WaybillShipmentAddRequest;
import com.nanyang.tms.api.service.waybill.WaybillCoreRpcService;
import com.nanyang.tms.dao.waybill.WaybillDao;
import com.nanyang.tms.service.waybill.impl.flow.WaybillProcessPipelineFactory;
import com.nanyang.tms.service.waybill.impl.flow.WaybillShipmentProcessPipelineFactory;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class WaybillCoreRpcServiceImpl implements WaybillCoreRpcService{
	@Autowired
	private WaybillProcessPipelineFactory waybillProcessPipelineFactory;
	@Autowired
	private WaybillShipmentProcessPipelineFactory waybillShipmentProcessPipelineFactory;
	@Autowired
	private WaybillDao waybillDao;
	@Override
	public ApiResult<Boolean> saveWaybill(WaybillAddReuqest waybillAddReuqest) {
		ApiResult<Boolean> apiResult = new  ApiResult<Boolean>();
		try {
			TransOutboundInvoker invoker = waybillProcessPipelineFactory.build(waybillAddReuqest);
			invoker.start();
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(true);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", waybillAddReuqest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}
	@Override
	public ApiResult<Boolean> saveWaybillShipment(WaybillShipmentAddRequest waybillShipmentAddRequest) {
		ApiResult<Boolean> apiResult = new  ApiResult<Boolean>();
		try {
			TransOutboundInvoker invoker = waybillShipmentProcessPipelineFactory.build(waybillShipmentAddRequest);
			invoker.start();
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(true);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", waybillShipmentAddRequest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
		
	}
	@Override
	public ApiResult<String> getCompanyNameBySupplierNo(String supplierNo) {
		ApiResult<String> apiResult = new  ApiResult<String>();
		try {
			String companyNameBySupplierNo = waybillDao.getCompanyNameBySupplierNo(supplierNo);
			if(StringUtils.isEmpty(companyNameBySupplierNo)){
				throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
			}
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(companyNameBySupplierNo);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", supplierNo.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}

}
